import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:testlzhu/page/basemian/state.dart';

import 'logic.dart';

class BasePage extends StatefulWidget {
  const BasePage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => BasePageState();
}

class BasePageState extends State<BasePage> {
  int currentIndex = 0;
  late PageController _pageController;

  @override
  void initState() {
    _pageController = PageController(keepPage: true);
  }

  @override
  Widget build(BuildContext context) {
    final logic = Get.put(MainLogic());
    return Scaffold(
        body: PageView(
          controller: _pageController,
          physics: const NeverScrollableScrollPhysics(),
          onPageChanged: (int index) {},
          children: logic.state.pageviews,
        ),
        bottomNavigationBar: myBottomAppBar(logic.state));
  }

  Widget myBottomAppBar(MainState state) {
    return BottomAppBar(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          SizedBox(height: 49, width: 49, child: bottomAppBarItem(0, state)),
          SizedBox(height: 49, width: 49, child: bottomAppBarItem(1, state)),
          SizedBox(height: 49, width: 49, child: bottomAppBarItem(2, state)),
          SizedBox(height: 49, width: 49, child: bottomAppBarItem(3, state)),
          SizedBox(height: 49, width: 49, child: bottomAppBarItem(4, state)),
        ],
      ),
    );
  }

  Widget bottomAppBarItem(int index, MainState state) {
//设置默认未选中的状态
    TextStyle style = const TextStyle(fontSize: 12, color: Colors.black);
    String imgUrl = state.normalImgUrls[index];
    if (currentIndex == index) {
      //选中的话
      style = const TextStyle(fontSize: 13, color: Colors.blue);
      imgUrl = state.selectedImgUrls[index];
    }
    //构造返回的Widget
    Widget item = GestureDetector(
      behavior: HitTestBehavior.opaque,
      child: Column(
        mainAxisAlignment: (index != 2) ? MainAxisAlignment.spaceEvenly : MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Image.asset(imgUrl, width: (index == 2) ? 45 : 25, height: (index == 2) ? 45 : 25),
          Visibility(
            visible: index != 2,
            child: Text(
              state.titles[index],
              style: style,
            ),
          )
        ],
      ),
      onTap: () {
        if (currentIndex != index && index != 2) {
          setState(() {
            currentIndex = index;
            _pageController.jumpToPage(currentIndex);
          });
        }
      },
    );
    return item;
  }
}
